package com.tnstart.android.control.light.db;

import java.util.List;

import com.tnstart.android.control.light.config.AreaBean;
import com.tnstart.android.control.light.config.ConfigBean;
import com.tnstart.android.control.light.config.ConfigLoader;
import com.tnstart.android.control.light.config.ItemBean;
import com.tnstart.android.control.light.config.ItemTypeBean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DBHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "tndb";
	private static final int DB_VERSION = 2;

	private SQLiteDatabase db;
	
	private static DBHelper mdbHelper;
	
	public static DBHelper getInstance(Context context)
	{
		if(mdbHelper==null)
		{
			mdbHelper=new DBHelper(context);
		}
		return mdbHelper;
	}

	private DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	private DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		operateTable(db, "");
		loadxmlConfig();
		loaditemConfig();
		loadareaConfig();
		loaditemTypeConfig();
	}
	
	private void loadxmlConfig(){
		ConfigBean configBean =ConfigLoader.loadConfigXml("/config.xml");
		ContentValues values = new ContentValues();
		values.put(ConfigColumn.HOST_NAME, configBean.getHost());
		values.put(ConfigColumn.PORT, configBean.getPort());
		this.insert(ConfigColumn.TABLE_NAME, values);
	}
	
	private void loaditemConfig(){
		List<ItemBean> itemsArr =ConfigLoader.loadItemXml("/item.xml");
		for(ItemBean itemBean : itemsArr){
			ContentValues values = new ContentValues();
			values.put(ItemColumn.ITEM_ID, itemBean.getItemId());
			values.put(ItemColumn.ITEM_DEFAULT_NAME, itemBean.getItemDefaultName());
			values.put(ItemColumn.ITEM_TYPE_ID, itemBean.getItemTypeId());
			values.put(ItemColumn.AREA_ID, itemBean.getAreaId());
			values.put(ItemColumn.NET_NUM, itemBean.getNetNum());
			values.put(ItemColumn.DEVICE_NUM, itemBean.getDeviceNum());
			values.put(ItemColumn.CONTROL_TYPE, itemBean.getControlType());
			values.put(ItemColumn.CIRCUIT_NUM, itemBean.getCircuitNum());
			this.insert(ItemColumn.TABLE_NAME, values);
		}
	}
	
	private void loadareaConfig(){
		List<AreaBean> areasArr =ConfigLoader.loadAreaXml("/area.xml");
		for(AreaBean areaBean : areasArr){
			ContentValues values = new ContentValues();
			values.put(AreaColumn.AREA_ID, areaBean.getAreaId());
			values.put(AreaColumn.AREA_NAME, areaBean.getAreaName());
			values.put(AreaColumn.AREA_TYPE_ID, areaBean.getAreaTypeId());
			this.insert(AreaColumn.TABLE_NAME, values);
		}
	}
	
	private void loaditemTypeConfig(){
		List<ItemTypeBean> itemTypeArr = ConfigLoader.loadItemTypeXml("/itemType.xml");
		for(ItemTypeBean itemTypeBean : itemTypeArr){
			ContentValues values = new ContentValues();
			values.put(ItemTypeColumn.ITEM_ID, itemTypeBean.getItemTypeId());
			values.put(ItemTypeColumn.ITEM_TYPE_NAME, itemTypeBean.getItemTypeName());
			this.insert(ItemTypeColumn.TABLE_NAME, values);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		if (oldVersion == newVersion) {
			return;
		}
		operateTable(db, "DROP TABLE IF EXISTS ");
		onCreate(db);
	}

	public void operateTable(SQLiteDatabase db, String actionString) {
		Class<DatabaseColumn>[] columnsClasses = DatabaseColumn.getSubClasses();
		DatabaseColumn columns = null;

		for (int i = 0; i < columnsClasses.length; i++) {
			try {
				columns = columnsClasses[i].newInstance();
				if ("".equals(actionString) || actionString == null) {
					db.execSQL(columns.getTableCreateor());
				} else {
					db.execSQL(actionString + columns.getTableName());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	public long insert(String Table_Name, ContentValues values) {
		if (db == null)
			db = getWritableDatabase();
		return db.insert(Table_Name, null, values);
	}

	/**
	 * 
	 * @param Table_Name
	 * @param id
	 * @return 影响行数
	 */
	public int delete(String Table_Name, int id) {
		if (db == null)
			db = getWritableDatabase();
		return db.delete(Table_Name, BaseColumns._ID + "=?",
				new String[] { String.valueOf(id) });
	}

	/**
	 * @param Table_Name
	 * @param values
	 * @param WhereClause
	 * @param whereArgs
	 * @return 影响行数
	 */
	public int update(String Table_Name, ContentValues values,
			String WhereClause, String[] whereArgs) {
		if (db == null) {
			db = getWritableDatabase();
		}
		return db.update(Table_Name, values, WhereClause, whereArgs);
	}

	public Cursor query(String Table_Name, String[] columns, String whereStr,
			String[] whereArgs) {
		if (db == null) {
			db = getReadableDatabase();
		}
		return db.query(Table_Name, columns, whereStr, whereArgs, null, null,
				null);
	}

	public Cursor rawQuery(String sql, String[] args) {
		if (db == null) {
			db = getReadableDatabase();
		}
		return db.rawQuery(sql, args);
	}

	public void ExecSQL(String sql) {
		if (db == null) {
			db = getWritableDatabase();
		}
		db.execSQL(sql);
	}

	public void closeDb() {
		if (db != null) {
			db.close();
			db = null;
		}
	}

}
